package com.hjt.gulimall.order.controller;

import java.util.*;

import com.hjt.common.to.order.OrderTo;
import com.hjt.gulimall.order.entity.OrderReturnReasonEntity;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.hjt.gulimall.order.entity.OrderEntity;
import com.hjt.gulimall.order.service.OrderService;
import com.hjt.common.utils.PageUtils;
import com.hjt.common.utils.R;

import javax.websocket.server.PathParam;


/**
 * 订单
 *
 * @author huangjingtai
 * @email 970075985@qq.com
 * @date 2021-09-08 20:05:31
 */
@RestController
@RequestMapping("order/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 分页查询登录用户的所有订单信息
     * @param params
     * @return
     */
    @PostMapping("/listWithPage")
    public R pageWithItem(@RequestBody Map<String, Object> params) {
        PageUtils pageUtils = orderService.pageWithItem(params);

        return R.ok().put("page", pageUtils);
    }

    @GetMapping("/sendMsg")
    public String testSendMessage() {
        OrderEntity object = new OrderEntity();
        object.setOrderSn(UUID.randomUUID().toString().replaceAll("-",""));
        // 发送一个对象，默认使用的序列化转换器，需要实现序列化接口
        rabbitTemplate.convertAndSend("order-event-exchange","order.create.order",object,new CorrelationData(UUID.randomUUID().toString()));
        return "ok";
    }

    /**
     * 根据订单号判断订单是否存在
     * @param orderSn 订单号
     * @return
     */
    @GetMapping("/getByOrderSn/{orderSn}")
    public R getOrderByOrderSn(@PathVariable("orderSn") String orderSn) {
        OrderEntity orderEntity =  orderService.getByOrderSn(orderSn);
        OrderTo orderTo = null;
        if (orderEntity != null) {
            orderTo = new OrderTo();
            BeanUtils.copyProperties(orderEntity, orderTo);
        }
        return R.ok().setData(orderTo);
    }




    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("order:order:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = orderService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("order:order:info")
    public R info(@PathVariable("id") Long id){
		OrderEntity order = orderService.getById(id);

        return R.ok().put("order", order);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("order:order:save")
    public R save(@RequestBody OrderEntity order){
		orderService.save(order);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("order:order:update")
    public R update(@RequestBody OrderEntity order){
		orderService.updateById(order);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("order:order:delete")
    public R delete(@RequestBody Long[] ids){
		orderService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
